﻿
var PopupMenu = function() {
    this.init();
}
PopupMenu.SEPARATOR = 'PopupMenu.SEPARATOR';
PopupMenu.current = null;
PopupMenu.popupPtr = null;
PopupMenu.addEventListener = function(element, name, observer, capture) {
    if (typeof element == 'string') {
        element = document.getElementById(element);
    }
    if (element.addEventListener) {
        element.addEventListener(name, observer, capture);
    } else if (element.attachEvent) {
        element.attachEvent('on' + name, observer);
    }
};
PopupMenu.prototype = {
    init: function() {
        this.items  = [];
        this.width  = 0;
        this.height = 0;
        this.popupPtr = null;
        this.popupWindowHeight = 0;
    },
    setSize: function(width, height) {
        //this.width  = width;
        //this.height = height;
//        
//        if (this.element) {
//            var self = this;
//            with (this.element.style) {
//                if (self.width)  width  = self.width  + 'px';
//                if (self.height) height = self.height + 'px';
//            }
//        }
    },
    bind: function(element) {
        var self = this;
        if (!element) {
            element = document;
        } else if (typeof element == 'string') {
            element = document.getElementById(element);
        }
        this.target = element;
        this.target.oncontextmenu = function(e) {
            self.show.call(self, e);
            return false;
        };
        var listener = function() { self.hide.call(self) };
        PopupMenu.addEventListener(document, 'click', listener, true);
    },
    add: function(text, callback,flag) {
        this.items.push({ text: text, callback: callback , flag:flag });
    },
    addSeparator: function() {
        this.items.push(PopupMenu.SEPARATOR);
    },
    setPos: function(x,y) {
        if (!this.element) return;
//        if (!e) e = window.event;
//        var x, y;
//        if (window.opera) {
//            x = e.clientX;
//            y = e.clientY;
//        } else if (document.all) {
//            x = document.body.scrollLeft + event.clientX;
//            y = document.body.scrollTop + event.clientY;
//        } else if (document.layers || document.getElementById) {
//            x = e.pageX;
//            y = e.pageY;
//        }
                
        var oPopup = window.createPopup();
        //this.popupPtr = oPopup;
        PopupMenu.popupPtr = oPopup;
        var oPopBody = oPopup.document.body;
        oPopBody.style.border = "solid black 1px";
        this.element.style.display = '';

        head=oPopup.document.getElementsByTagName('head').item(0);
        script=oPopup.document.createElement('script');
        script.src=top.data.webpath+"/js/PopupMenu.js";
        script.type="text/javascript";
        head.appendChild(script);

       
        
        oPopBody.innerHTML = this.element.outerHTML ;
        oPopBody.noWrap = true;
        this.width = 0;
        oPopup.show(x , y, this.width, this.popupWindowHeight, document.body);
        
        //this.element.style.top  = y + 'px';
        //this.element.style.left = x + 'px';
        this.width = oPopup.document.body.scrollWidth;
        //debugger;
        oPopup.show(x , y, this.width, this.popupWindowHeight, document.body);
        
    },
    show: function(x,y) {
    
        if (PopupMenu.popupPtr )
        {
            
            PopupMenu.popupPtr.hide();
        } 
        
        //PopupMenu.current = this;
 
        if (this.element) {
            this.setPos(x,y);
           // this.element.style.display = '';
        } else {
            this.element = this.createMenu(this.items);
            this.setPos(x,y);
            //document.body.appendChild(this.element);
        }
        
    },
    hide: function() {
    
        PopupMenu.current.popupPtr.hide();
        PopupMenu.current = null;
        //if (this.element) this.element.style.display = 'none';
    },
    createMenu: function(items) {
        var self = this;

        
        var menu = document.createElement('div');
        menu.id = "menu";
        
        with (menu.style) {
        
            //if (self.width)  width  = self.width  + 'px';
           // if (self.height) height = self.height + 'px';
            //border     = "1px solid gray";
            background = "#D9F2EF";
            wordBreak = "keep-all";
            position   = 'absolute';
            display    = 'block';
           
//            paddingLeft = "16px";
//            paddingTop = "4px";
            cursor     = 'default';
            fontFamily  = "Arial";
            fontSize   = '12px';
        }
        
        for (var i = 0; i < items.length; i++) {
            var item;
            if (items[i] == PopupMenu.SEPARATOR) {
                item = this.createSeparator();
                this.popupWindowHeight += 2;
                
            } else {
                item = this.createItem(items[i]);
                this.popupWindowHeight += 22;
            }
            menu.appendChild(item);
        }
        return menu;
    },
    createItem: function(item) {
        var self = this;
        var elem = document.createElement('div');
        //elem.style.padding = '4px';
        elem.style.wordBreak = "keep-all";
        elem.style.backgroundImage = "url(images/subnav_01.gif)";
        elem.style.backgroundRepeat = "no-repeat";
        elem.style.backgroundPositionX = "14px";
        elem.style.backgroundPositionY = "8px";
        elem.style.paddingLeft = "12px";
        elem.style.paddingRight = "12px";
        elem.style.paddingTop = "3px";
        elem.style.paddingBottom = "3px"
        elem.style.height = "22px"
        var callback = item.callback;
        
        with(elem) {
            
            onmouseover = "this.style.background = '#fff';" + 
                          "this.style.backgroundImage = 'url(images/subnav_02.gif)';" +
                          "this.style.backgroundRepeat = 'no-repeat';" +
                          "this.style.backgroundPositionX = '14px';" +
                          "this.style.backgroundPositionY = '8px';" +
                          "style.color = '#00A88F'";
            onmouseout = "this.style.background = '#D9F2EF';" +
                          "this.style.backgroundImage = 'url(images/subnav_01.gif)';" +
                          "this.style.backgroundRepeat = 'no-repeat';" +
                          "this.style.backgroundPositionX = '14px';" +
                          "this.style.backgroundPositionY = '8px';"  +
                          "style.color = '#000'";
            
            if(item.flag == "mouseOver")
            {
                
               onmouseover = onmouseover + ";" + callback;
            }
            else           
            {
                onclick = callback;
                
            }
            
                          
        }
        elem.appendChild(document.createTextNode(item.text));
        return elem;
    },
    createSeparator: function() {
        var sep = document.createElement('div');
        with (sep.style) {
            borderTop = '1px dotted #CCCCCC';
            fontSize  = '0px';
            height    = '0px';
        }
        return sep;
    }
};



 function subPopupMenu(hierarchy,positionX,positionY,div)
        {  
           
           var popup = new PopupMenu();
	       var lever = hierarchy.split("-");
	       var popMenu;
	       var popHierarchy;
	       var subPositon = {x:0,y:0};
	       popMenu = top.data.menuList[lever[0]];
	       for(var i=1;i<lever.length;i++)
	       {
	            popMenu = popMenu.subMenu[lever[i]];
	       }
	       
            for(var j = 0;j<popMenu.subMenu.length;j++)
            {
                popHierarchy = hierarchy + "-" + j; 
                if(popMenu.subMenu[j].subMenu.length==0)
                {                                
                    popup.add(popMenu.subMenu[j].menuName,"top.executeMenuAction('" + popHierarchy + "')","");
                   
                }
                
                else
                {
                    
                    subPositon.x =  div.parentElement.offsetWidth;
                    subPositon.y =  22*j;
                    popup.add(popMenu.subMenu[j].menuName,"subPopupMenu('" + popHierarchy + "','" + subPositon.x +"','" + subPositon.y +"',this)","mouseOver"); 
                    
                }
                
            }
            
            popup.setSize(160, 0);
            popup.show(div.parentElement.offsetWidth ,positionY);
            
        }